package envs

//符号表
//创建嵌套环境，保留对外层环境的引用
func NewEnclosedEnvironment(outer *Environment, name string) *Environment {
	env := NewEnvironment()
	env.name = name
	env.outer = outer
	return env
}
func NewEnvironment() *Environment {
	s := make(map[string]Symbol)
	return &Environment{store: s, outer: nil}
}

// Symbol 表示一个符号
type Symbol struct {
	Name   string
	Type   string
	Module string
	Value  interface{} // 可选：符号的值
}

type Environment struct {
	name  string
	store map[string]Symbol //map
	outer *Environment      //指向外层环境的指针，实现作用域链
}

func (e *Environment) Get(name string) (Symbol, bool) {
	obj, ok := e.store[name]
	if !ok && e.outer != nil {
		obj, ok = e.outer.Get(name)
	}
	return obj, ok
}

func (e *Environment) Set(name string, val Symbol) Symbol {
	e.store[name] = val
	return val
}
